package com.bdqn.service.Impl;

import com.bdqn.dao.AdminQueryDao;
import com.bdqn.dao.Impl.AdminQueryDaoImpl;
import com.bdqn.entity.CheckWork;
import com.bdqn.entity.Employee;
import com.bdqn.entity.PageBean;
import com.bdqn.service.AdminService;

import java.util.List;

public class AdminServiceImpl implements AdminService {
    private AdminQueryDao dao = new AdminQueryDaoImpl();
    @Override
    public PageBean<CheckWork> findAll(int currentPage, int pageSize) {
        PageBean<CheckWork> pb = new PageBean<>();
        pb.setCurrentPage(currentPage);
        pb.setPageSize(pageSize);
        int totalCount = dao.findByAll();
        pb.setTotalCount(totalCount);
        int totalPage = totalCount%pageSize==0 ?totalCount/pageSize:(totalCount/pageSize)+1;
        pb.setTotalPage(totalPage);
        int offset = (currentPage-1)*pageSize;
        pb.setList(dao.findByPage(offset,pageSize));
        return pb;
    }

    @Override
    public int updateAll(String date) {
        List<Employee> emp = dao.findEmp();
        int insert = 0;
        for (Employee employee:emp){
            int count = dao.finByIdAndDate(employee.getEmpId(),date);
            if(count == 0){
                insert = dao.insert(employee.getEmpId(), date);
            }
        }
        return insert;
    }

    @Override
    public PageBean<CheckWork> FindPageQuery(int currentPage, int pageSize, String empId, int depId, String start, String end) {
        PageBean<CheckWork> pb = new PageBean<>();
        pb.setCurrentPage(currentPage);
        pb.setPageSize(pageSize);
        int totalCount = dao.findByCondition(empId,depId,start,end);
        pb.setTotalCount(totalCount);
        int totalPage = totalCount%pageSize==0 ?totalCount/pageSize:(totalCount/pageSize)+1;
        pb.setTotalPage(totalPage);
        int offset = (currentPage-1)*pageSize;
        pb.setList(dao.FindPageQueryByCond(offset,pageSize,empId,depId,start,end));
        return pb;
    }

    @Override
    public boolean update(int week, int depId, String start, String end) {
        if(dao.findway(week,depId)>0){

            return dao.updateway(week,depId,start,end)>0;
        }else {
            return false;
        }
    }

    @Override
    public boolean addWay(int week, int depId, String start, String end) {
        if(dao.findway(week,depId) > 0){
            return false;
        }else{
            return dao.addway(week,depId,start,end)>0;
        }
    }
}
